/**
 * 
 */
package data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONObject;

import map.City;
import map.Location;
import map.Station;
import map.Train;
import map.Waypoint;

/**
 * @author Mathias
 *
 */
public class Parser {

	static SimpleDateFormat nextrunSDF = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");
	static SimpleDateFormat finishedSDF = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");

	public static HashMap<Integer, Station> parseStations(JSONObject json){
		HashMap<Integer, Station> stations = new HashMap<Integer, Station>();	
		for (Iterator<String> i = json.keys(); i.hasNext();){
			JSONObject c = json.getJSONObject(i.next());

			if (c.has("lon")){
				Location l = new Location(c.getDouble("lat"), c.getDouble("lon"));
				Station s = new Station(c.getInt("id"), l, c.getString("name"));
				stations.put(s.getId(), s);
			}	
		}
		return stations;
	}

	public static HashMap<Integer, City> parseCities(JSONObject json){
		return null;
	}

	public static HashMap<String, Train> parseTrains(JSONObject json){
		HashMap<String, Train> trains = new HashMap<String, Train>();
		for (Iterator<String> i = json.keys(); i.hasNext();){
			JSONObject c = json.getJSONObject(i.next());
			//date
			JSONArray d = (JSONArray) c.get("date");
			Calendar date = Calendar.getInstance();
			date.set(d.getInt(0), d.getInt(1), d.getInt(2));
			
			//nextrun
			String n = c.getString("nextrun");
			Calendar nextrun = Calendar.getInstance();
			try {
				nextrun.setTime(nextrunSDF.parse(n));
			} catch (ParseException e) {
				throw new IllegalArgumentException("wrong date format");
			}

			//finished
			Calendar finished = Calendar.getInstance();
		
			Object f = c.get("finished");

			if (f != JSONObject.NULL){
				String finishedString = c.getString("finished");
				finishedString = finishedString.substring(0, finishedString.length()-6);
				try {
					finished.setTime(finishedSDF.parse(finishedString));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			else{
				finished.setTimeInMillis(0);
			}
			
			//started
			Calendar started = Calendar.getInstance();
		
			Object s = c.get("started");

			if (s != JSONObject.NULL){
				String startedString = c.getString("started");
				startedString = startedString.substring(0, startedString.length()-6);
				try {
					started.setTime(finishedSDF.parse(startedString));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			else{
				started.setTimeInMillis(0);
			}
			
			String status = c.getString("status");

			//train id
			String id = c.getString("train_nr");
			
			JSONArray stations =  (JSONArray) c.get("stations");
			
			trains.put(id, new Train(id, date, status, started, finished, nextrun, stations));
		}

		return trains;
	}

	public static LinkedList<Waypoint> parseWaypoints(JSONArray stations) {
		LinkedList<Waypoint> waypoints = new LinkedList<Waypoint>();
		for (int i = 0; i < stations.length(); i++){
			JSONObject s = (JSONObject) stations.get(i);
			int arrival = -1;
			int departure = -1;
			
			if (s.has("departure")){
				departure = s.getInt("departure");
			}
			
			if (s.has("arrival")){
				arrival = s.getInt("arrival");
			}
			
			int station = s.getInt("station_id");
			
			Waypoint w = new Waypoint(station, departure, arrival);
			
			if(s.has("delay")){
				w.setDelay(s.getInt("delay"));
			}
			
			waypoints.add(w);
		}
		
		return waypoints;
	}

}
